package com.xy.config.mqtt;

import com.xy.service.mqtt.IMqttService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;

@Slf4j
@Configuration
public class MqttConfig implements CommandLineRunner {

    @Autowired
    private MqttProperties mqttProperties;

    @Autowired
    private IMqttService mqttService;

    @Override
    public void run(String... args) throws Exception {
        String clientId = String.format("server:%s@8050", InetAddress.getLocalHost());
        MessageClient.init(mqttProperties.getTcpUrl(), clientId, true, mqttProperties.getResponseTimes(), mqttService.appGet(clientId));
        if (!mqttProperties.isSubscribe()) {
            return;
        }
        MessageClient.getInstance().setListener(new IListener() {
            @Override
            public void onConnected() {
                log.info("onConnected");
            }

            @Override
            public void onError(Throwable cause) {
                log.info("onError", cause);
            }
        });
    }
}
